package com.entfrm.biz.it.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entfrm.biz.it.entity.Property;
import com.entfrm.biz.it.entity.PropertyCategory;
import com.entfrm.biz.it.excel.PropertyExcel;
import com.entfrm.biz.it.excel.PropertyExcelVo;
import com.entfrm.biz.it.manager.PropertyManager;
import com.entfrm.biz.it.service.PropertyCategoryService;
import com.entfrm.biz.it.service.PropertyService;
import com.entfrm.biz.system.entity.User;
import com.entfrm.core.log.annotation.OperLog;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import com.entfrm.core.base.api.R;
import com.entfrm.core.base.util.ExcelUtil;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.io.FileOutputStream;
import java.util.*;

/**
 * @author kyc
 * @date 2020-08-07 17:56:35
 * @description 资产信息Controller
 */
@Api("资产信息管理")
@RestController
@AllArgsConstructor
@RequestMapping("/it/property")
public class PropertyController {

    private final PropertyManager propertyManager;

    private final PropertyService propertyService;

    private QueryWrapper<Property> getQueryWrapper(Property property) {
        return new QueryWrapper<Property>()
                .like(StrUtil.isNotBlank(property.getCowinNum()), "cowin_num", property.getCowinNum())
                .or()
                .eq(ObjectUtil.isNotNull(property.getCategoryId()), "category_id", property.getCategoryId())
                .or()
                .like(ObjectUtil.isNotNull(property.getCategoryId()), "category_ids", property.getCategoryId())
                .like(StrUtil.isNotBlank(property.getBrand()), "brand", property.getBrand())
                .like(StrUtil.isNotBlank(property.getModelNum()), "model_num", property.getModelNum())
                .like(StrUtil.isNotBlank(property.getLocation()), "location", property.getLocation())
                .like(StrUtil.isNotBlank(property.getStatus()), "status", property.getStatus())
                .between(StrUtil.isNotBlank(property.getBeginTime()) && StrUtil.isNotBlank(property.getEndTime()), "in_time", property.getBeginTime(), property.getEndTime())
                .orderByDesc("create_time");
    }

    @ApiOperation("资产信息列表")
    @PreAuthorize("@ps.hasPerm('property_view')")
    @GetMapping("/list")
    public R list(Page page, Property property) {
        IPage<Property> propertyPage = propertyManager.pageProperty(page, getQueryWrapper(property));
        return R.ok(propertyPage.getRecords(), propertyPage.getTotal());
    }

    @ApiOperation("资产信息查询")
    @PreAuthorize("@ps.hasPerm('property_detail')")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        Property property = propertyManager.getPropertyById(id);
        return R.ok(property);
    }

    @OperLog("资产信息新增")
    @ApiOperation("资产信息新增")
    @PreAuthorize("@ps.hasPerm('property_add')")
    @PostMapping("/save")
    public R save(@Validated @RequestBody Property property) {
        return propertyManager.saveProperty(property);
    }

    @OperLog("资产信息修改")
    @ApiOperation("资产信息修改")
    @PreAuthorize("@ps.hasPerm('property_edit')")
    @PutMapping("/update")
    public R update(@Validated @RequestBody Property property) {
        return propertyManager.updatePropertyById(property);
    }

    @OperLog("修改资产状态待报废")
    @ApiOperation("修改资产状态待报废")
    @PreAuthorize("@ps.hasPerm('property_wait_scrap')")
    @PutMapping("/waitScrapProperty")
    public R waitScrapProperty(@Validated @RequestBody Property property) {
        propertyManager.waitScrapProperty(property);
        return R.ok();
    }

    @OperLog("修改资产状态维修中")
    @ApiOperation("修改资产状态维修中")
    @PreAuthorize("@ps.hasPerm('property_in_service')")
    @PutMapping("/inServiceProperty")
    public R inServiceProperty(@Validated @RequestBody Property property) {
        propertyManager.inServiceProperty(property);
        return R.ok();
    }

    @OperLog("修改资产状态在库(恢复正常)")
    @ApiOperation("修改资产状态在库(恢复正常)")
    @PreAuthorize("@ps.hasPerm('property_in_stock')")
    @PutMapping("/inStockProperty")
    public R inStockProperty(@Validated @RequestBody Property property) {
        propertyManager.inStockProperty(property);
        return R.ok();
    }

    @OperLog("资产信息删除")
    @ApiOperation("资产信息删除")
    @PreAuthorize("@ps.hasPerm('property_del')")
    @DeleteMapping("/remove/{ids}")
    public R remove(@PathVariable("ids") Integer[] ids) {
        return R.ok(propertyService.removeByIds(Arrays.asList(ids)));
    }


    @PreAuthorize("@ps.hasPerm('property_export')")
    @GetMapping("/export")
    public R export(Property property) {
        List<PropertyExcel> list = propertyManager.listPropertyExcel(property);
        ExcelUtil<PropertyExcel> util = new ExcelUtil<PropertyExcel>(PropertyExcel.class);
        return util.exportExcel(list, "资产信息数据");
    }

    @SneakyThrows
    @OperLog("用户数据导入")
    @PreAuthorize("@ps.hasPerm('property_import')")
    @PostMapping("/importProperty")
    public R importUser(MultipartFile file, boolean updateSupport) {
        ExcelUtil<Property> util = new ExcelUtil<Property>(Property.class);
        List<Property> propertyList = util.importExcel(file.getInputStream());
        String message = propertyService.importProperty(propertyList, updateSupport);
        return R.ok(message);
    }

    @GetMapping("/importTemplate")
    public R importTemplate() {
        ExcelUtil<Property> util = new ExcelUtil<Property>(Property.class);
        return util.importTemplateExcel("资产数据");
    }

}
